package com.xingge.sdktest.convert;

import android.content.Context;

import com.jolimark.UsbTool;

import java.util.Arrays;
import java.util.concurrent.LinkedBlockingDeque;

public class ConvertPdfThread extends Thread {


    private boolean cancel;
    private ConvertImgThread convertImgThread;
    private Context context;
    private PdfTask task;

    public ConvertPdfThread(Context context) {
        this.context = context;
    }

    public void bindConvertImgThread(ConvertImgThread thread) {
        convertImgThread = thread;
    }

    public void setTask(PdfTask task) {
        this.task = task;
    }


    public void cancelTask() {
        cancel = true;
    }

    @Override
    public void run() {
        super.run();
        UsbTool.logd("convertPDF thread -> 线程启动");
        if (task != null) {
            String printRange = task.printRange;
            boolean flag_range = false;
            LinkedBlockingDeque<Integer> rangeQueue = new LinkedBlockingDeque<Integer>();
            //解析打印页码范围字符串
            if (printRange != null) {
                String[] groups = printRange.split(",");
                if (groups != null && groups.length > 0) {
                    for (String str : groups) {
                        //包含多个字符 如：5-10
                        if (str.contains("-") && str.indexOf("-") == str.lastIndexOf("-")) {//包含“-”且第一个“-”与最后一个“-”的坐标一致，即只有一个“-”
                            String[] strings = str.split("-");
                            int start = Integer.valueOf(strings[0]);
                            int end = Integer.valueOf(strings[1]);
                            if (end <= start)
                                continue;
                            for (int i = start; i <= end; i++) {
                                try {
                                    rangeQueue.put(i);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                            UsbTool.logd("index list： " + Arrays.toString(rangeQueue.toArray()));
                        }
                        //只有单个字符
                        else if (str.length() == 1) {
                            int i = Integer.valueOf(str);
                            try {
                                rangeQueue.put(i);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    if (!rangeQueue.isEmpty()) {
                        flag_range = true;
                    }
                }
            }

            String pdfPath = task.pdfPath;
            PrintConverter.PrintParams printParams = task.printParams;
            PrintConverter.SideType sideType = printParams == null ? PrintConverter.SideType.SINGLE : printParams.sideType;

            //局部范围打印
            if (flag_range) {
                UsbTool.logd("convertPDF thread-> 局部范围转换");
                if (rangeQueue != null) {
                    if (sideType == PrintConverter.SideType.SINGLE) {
                        //循环提取队列中的页码，直到所有页码处理完毕
                        while (!rangeQueue.isEmpty() && !cancel) {
                            int index = rangeQueue.poll();
                            String ret = PrintConverter.pdf2tga(context, pdfPath, index, PrintConverter.ImgType.TGA);
                            //如果页码有效，转换成功则发送
                            if (ret != null) {
                                UsbTool.logd("convertPDF thread->pdf 第" + index + "页转换完成");
                                if (convertImgThread != null) {
                                    ConvertImgThread.ImgTask imgTask = new ConvertImgThread.ImgTask();
                                    imgTask.flag_end = false;
                                    imgTask.sideType = sideType;
                                    imgTask.taskObject = ret;
                                    imgTask.printParams = printParams;
                                    imgTask.pageIndex = new int[]{index, 0};
                                    convertImgThread.putTask(imgTask);
                                }
                            }
                        }
                        if (!cancel && convertImgThread != null) {
                            ConvertImgThread.ImgTask imgTask = new ConvertImgThread.ImgTask();
                            imgTask.flag_end = true;
                            convertImgThread.putTask(imgTask);
                        }

                    } else if (sideType == PrintConverter.SideType.DOUBLE) {
                        String[] pair = new String[2];
                        int[] indexPair = new int[2];
                        int pi = 0;
                        //循环提取队列中的页码，直到所有页码处理完毕
                        while (!rangeQueue.isEmpty() && !cancel) {
                            int index = rangeQueue.poll();
                            String ret = PrintConverter.pdf2tga(context, pdfPath, index, PrintConverter.ImgType.TGA);
                            //如果页码有效，转换成功
                            if (ret != null) {
                                UsbTool.logd("convertPDF thread->pdf 第" + index + "页转换tga完成");
                                //记录转换的图片地址到pair中
                                pair[pi] = ret;
                                indexPair[pi] = index;
                                pi++;
                                //如果pair已经保存有一对图片地址，则发送
                                if (pi == 2) {
                                    UsbTool.logd("convertPDF thread->pdf 传递一对图片");
                                    if (convertImgThread != null) {
                                        ConvertImgThread.ImgTask imgTask = new ConvertImgThread.ImgTask();
                                        imgTask.flag_end = false;
                                        imgTask.sideType = sideType;
                                        imgTask.taskObject = pair;
                                        imgTask.printParams = printParams;
                                        imgTask.pageIndex = indexPair;
                                        convertImgThread.putTask(imgTask);
                                    }
                                    //重置pair以便下次使用,因为是传递引用给另一个线程，所以只能新建一个pair数组供下次使用
                                    pi = 0;
                                    pair = new String[2];
                                    indexPair = new int[2];
                                }
                            }
                        }
                        //转换后有效的图片剩余1张不能组合成对
                        if (!cancel && pi == 1) {
                            UsbTool.logd("convertPDF thread->pdf 传递剩余1张图片");
                            if (convertImgThread != null) {
                                ConvertImgThread.ImgTask imgTask = new ConvertImgThread.ImgTask();
                                imgTask.flag_end = false;
                                imgTask.sideType = sideType;
                                imgTask.taskObject = pair;
                                imgTask.pageIndex = indexPair;
                                imgTask.printParams = printParams;
                                convertImgThread.putTask(imgTask);
                            }
                        }
                        if (!cancel && convertImgThread != null) {
                            ConvertImgThread.ImgTask imgTask = new ConvertImgThread.ImgTask();
                            imgTask.flag_end = true;
                            convertImgThread.putTask(imgTask);
                        }

                    }
                }

            }
            //全部打印
            else {
                UsbTool.logd("convertPDF thread-> 全部转换");
                if (sideType == PrintConverter.SideType.SINGLE) {
                    int index = 1;
                    while (!cancel) {
                        String ret = PrintConverter.pdf2tga(context, pdfPath, index, PrintConverter.ImgType.TGA);
                        if (ret == null) {
                            UsbTool.logd("convertPDF thread->pdf转tga任务结束");
                            if (convertImgThread != null) {
                                ConvertImgThread.ImgTask imgTask = new ConvertImgThread.ImgTask();
                                imgTask.flag_end = true;
                                convertImgThread.putTask(imgTask);
                            }
                            break;
                        } else {
                            UsbTool.logd("convertPDF thread->pdf 第" + index + "页转换tga完成");
                            if (convertImgThread != null) {
                                ConvertImgThread.ImgTask imgTask = new ConvertImgThread.ImgTask();
                                imgTask.flag_end = false;
                                imgTask.sideType = sideType;
                                imgTask.taskObject = ret;
                                imgTask.pageIndex = new int[]{index, 0};
                                imgTask.printParams = printParams;
                                convertImgThread.putTask(imgTask);
                            }
                            index++;
                        }
                    }
                } else if (sideType == PrintConverter.SideType.DOUBLE) {
                    int index = 1;
                    String[] pair = new String[2];
                    int[] indexPair = new int[2];
                    while (!cancel) {
                        String ret = PrintConverter.pdf2tga(context, pdfPath, index, PrintConverter.ImgType.TGA);
                        if (index % 2 == 0) {
                            if (ret == null) {
                                pair[1] = null;
                                indexPair[1] = 0;
                                UsbTool.logd("convertPDF thread->传递剩下的一张图片");
                                if (convertImgThread != null) {
                                    ConvertImgThread.ImgTask imgTask = new ConvertImgThread.ImgTask();
                                    imgTask.flag_end = false;
                                    imgTask.sideType = sideType;
                                    imgTask.taskObject = pair;
                                    imgTask.pageIndex = indexPair;
                                    imgTask.printParams = printParams;
                                    convertImgThread.putTask(imgTask);
                                    imgTask = new ConvertImgThread.ImgTask();
                                    imgTask.flag_end = true;
                                    convertImgThread.putTask(imgTask);
                                }
                                break;
                            } else {
                                pair[1] = ret;
                                indexPair[1] = index;
                                UsbTool.logd("convertPDF thread->第" + index + "页转换tga完成, 传递一对图片");
                                if (convertImgThread != null) {
                                    ConvertImgThread.ImgTask imgTask = new ConvertImgThread.ImgTask();
                                    imgTask.flag_end = false;
                                    imgTask.sideType = sideType;
                                    imgTask.taskObject = pair;
                                    imgTask.pageIndex = indexPair;
                                    imgTask.printParams = printParams;
                                    convertImgThread.putTask(imgTask);
                                }
                                //因为数组的引用已经交给转换线程，那么下次必须新建一个数组，并重新指向
                                pair = new String[2];
                                indexPair = new int[2];
                                index++;
                            }
                        } else {
                            if (ret == null) {
                                if (convertImgThread != null) {
                                    ConvertImgThread.ImgTask imgTask = new ConvertImgThread.ImgTask();
                                    imgTask.flag_end = true;
                                    convertImgThread.putTask(imgTask);
                                }
                                break;
                            } else {
                                pair[0] = ret;
                                indexPair[0] = index;
                                UsbTool.logd("convertPDF thread->pdf 第" + index + "页转换tga完成");
                                index++;
                            }
                        }
                    }


                }
            }
            if (cancel)
                UsbTool.logd("convertPDF thread->任务取消，线程结束");
            else
                UsbTool.logd("convertPDF thread->全部任务完成，线程结束");
        }

    }


    public static class PdfTask {
        String pdfPath;
        String printRange;
        PrintConverter.PrintParams printParams;
    }


}
